package com.aiden.cashbook.bean;

import android.os.Parcel;
import android.os.Parcelable;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Keep;
import org.greenrobot.greendao.annotation.ToOne;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.DaoException;
import com.aiden.cashbook.dao.DaoSession;
import com.aiden.cashbook.dao.UseTypeDao;
import com.aiden.cashbook.dao.BillDao;
import org.greenrobot.greendao.annotation.NotNull;
import com.aiden.cashbook.dao.UserDao;

/**
 * 账单内容实体
 * @author aiden
 * @date 2018/9/4 13:59
 */
@Entity
public class Bill implements Parcelable {

    @Id(autoincrement = true)
    private transient long id;

    private long userId; // 用户id
    @ToOne(joinProperty = "userId")
    private User user;

    private String type; // income expense

    private double money;

    private long useTypeId; // 用途id
    @ToOne(joinProperty = "useTypeId")
    private UseType useType;


    private String date; // yyyy-MM-dd

    private String remark;

    private String images;

    private double latitude;

    private double longitude;

    private String location;


    protected Bill(Parcel in) {
        type = in.readString();
        userId = in.readLong();
        money = in.readDouble();
        useTypeId = in.readLong();
        date = in.readString();
        remark = in.readString();
        images = in.readString();
        latitude = in.readDouble();
        longitude = in.readDouble();
        location = in.readString();
    }
    @Keep
    @Generated(hash = 1643441641)
    public Bill(long userId, String type, double money, long useTypeId, String date, String remark,
            String images, double latitude, double longitude, String location) {
        this.userId = userId;
        this.type = type;
        this.money = money;
        this.useTypeId = useTypeId;
        this.date = date;
        this.remark = remark;
        this.images = images;
        this.latitude = latitude;
        this.longitude = longitude;
        this.location = location;
    }

    @Generated(hash = 1399599325)
    public Bill() {
    }

    public static final Creator<Bill> CREATOR = new Creator<Bill>() {
        @Override
        public Bill createFromParcel(Parcel in) {
            return new Bill(in);
        }

        @Override
        public Bill[] newArray(int size) {
            return new Bill[size];
        }
    };

    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    @Generated(hash = 318225070)
    private transient BillDao myDao;

    @Generated(hash = 1127450150)
    private transient Long useType__resolvedKey;

    @Generated(hash = 251390918)
    private transient Long user__resolvedKey;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(type);
        dest.writeLong(userId);
        dest.writeDouble(money);
        dest.writeLong(useTypeId);
        dest.writeString(date);
        dest.writeString(remark);
        dest.writeString(images);
        dest.writeDouble(latitude);
        dest.writeDouble(longitude);
        dest.writeString(location);
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getMoney() {
        return this.money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public long getUseTypeId() {
        return this.useTypeId;
    }

    public void setUseTypeId(long useTypeId) {
        this.useTypeId = useTypeId;
    }

    public String getDate() {
        return this.date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getRemark() {
        return this.remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getImages() {
        return this.images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getLocation() {
        return this.location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Bill{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", money=" + money +
                ", useTypeId=" + useTypeId +
                ", useType=" + getUseType() +
                ", date='" + date + '\'' +
                ", remark='" + remark + '\'' +
                ", images='" + images + '\'' +
                ", latitude=" + latitude +
                ", longitude=" + longitude +
                ", location='" + location + '\'' +
                '}';
    }

    /** To-one relationship, resolved on first access. */
    @Generated(hash = 774354056)
    public UseType getUseType() {
        long __key = this.useTypeId;
        if (useType__resolvedKey == null || !useType__resolvedKey.equals(__key)) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            UseTypeDao targetDao = daoSession.getUseTypeDao();
            UseType useTypeNew = targetDao.load(__key);
            synchronized (this) {
                useType = useTypeNew;
                useType__resolvedKey = __key;
            }
        }
        return useType;
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 105532635)
    public void setUseType(@NotNull UseType useType) {
        if (useType == null) {
            throw new DaoException(
                    "To-one property 'useTypeId' has not-null constraint; cannot set to-one to null");
        }
        synchronized (this) {
            this.useType = useType;
            useTypeId = useType.getId();
            useType__resolvedKey = useTypeId;
        }
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 173933155)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getBillDao() : null;
    }

    public long getUserId() {
        return this.userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    /** To-one relationship, resolved on first access. */
    @Generated(hash = 115391908)
    public User getUser() {
        long __key = this.userId;
        if (user__resolvedKey == null || !user__resolvedKey.equals(__key)) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            UserDao targetDao = daoSession.getUserDao();
            User userNew = targetDao.load(__key);
            synchronized (this) {
                user = userNew;
                user__resolvedKey = __key;
            }
        }
        return user;
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 462495677)
    public void setUser(@NotNull User user) {
        if (user == null) {
            throw new DaoException(
                    "To-one property 'userId' has not-null constraint; cannot set to-one to null");
        }
        synchronized (this) {
            this.user = user;
            userId = user.getId();
            user__resolvedKey = userId;
        }
    }
}
